/**
 * 
 */
package com.EstateManager.Dao;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.EstateManager.Entity.Estatebasicinfo;
import com.EstateManager.Entity.Estateschedual;
import com.EstateManager.Entity.Leaseorsaleinfo;

/**
 * 创建时间 : 下午9:25:30
 * @author Yonggang Liu
 * 处理多数据表的数据操作
 *
 *
 *
 * MultiTableDao.java
 */
@Repository("multiTableDao")
public class MultiTableDao {

	/**
	 * 
	 */
	public MultiTableDao() {
	}
	
	@Autowired
	private LeaseOrSaleDao leaseOrSaleDao;
	
	@Autowired
	SessionFactory sessionFactory; 
	
	@Autowired
	EstateBasicInfoDao estateBasicInfoDao;
	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	/** 根据id取消租售信息发布，同时设置预订表的结果为失败 */
	public void cancelLeaseorsale(int id) throws HibernateException{
		Leaseorsaleinfo leaseorsaleinfo = leaseOrSaleDao.get(id);
		leaseorsaleinfo.setStatue(LeaseOrSaleDao.STATUE_CANCEL);
		leaseOrSaleDao.update(leaseorsaleinfo);
		Estatebasicinfo estatebasicinfo =  estateBasicInfoDao.get(leaseorsaleinfo.getEstatebasicinfo().getId());
		estatebasicinfo.setPublicStatu(EstateBasicInfoDao.PUBCLICSTATUE_NOPUBLIC);
		Query query = this.getCurrentSession().createQuery(""
				+ "update Estateschedual estateschedual "
				+ "set estateschedual.schedualresult = :schedualresult "
				+ "where estateschedual.leaseorsaleinfo = :leaseorsaleinfo");
		query.setParameter("schedualresult", EstateSchedualDao.SCHEDUAL_FAIL);
		query.setParameter("leaseorsaleinfo", leaseorsaleinfo);
		query.executeUpdate();
	}

}
